<?php

namespace App\Models;

use Carbon\Carbon;
use DB;
use Illuminate\Database\Eloquent\Model;

/**
 * App\Models\Comment
 *
 * @property int $id
 * @property int $uid 用户id
 * @property int $goods_id 商品id
 * @property string $comment 评论内容
 * @property string $avatar 用户头像
 * @property \Carbon\Carbon|null $created_at
 * @property \Carbon\Carbon|null $updated_at
 * @property-read \App\Models\Goods $goods
 * @property-read \App\Models\Customer $user
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Comment whereAvatar($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Comment whereComment($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Comment whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Comment whereGoodsId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Comment whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Comment whereUid($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Comment whereUpdatedAt($value)
 * @mixin \Eloquent
 */
class Comment extends Model
{
    protected $table='user_comments';

    protected $guarded=[];

    public function user()
    {
        return $this->belongsTo('App\Models\Customer','uid');
    }

    public function goods()
    {
        return $this->belongsTo('App\Models\Goods','goods_id');
    }

    public function destroyData($id)
    {
        $result = $this->destroy($id);
        if($result){
            return true;
        }else{
            return false;
        }
    }

    public static function  addComment($data)
    {
//        $input = [];
//        $input = ApiController::createData ($data);
//        $flag    = self::insert ($input);
//        return $flag;

        $insertData = [];
        $order_goods_list = DB::table('order_goods')
            -> where('order_id','=',$data['order_goods_id'])
            -> get() ->map(function($value){
                return (array) $value;
            })-> toArray();


        foreach($order_goods_list as $goods){
           $insertData[] = [
               'goods_id'=>$goods['goods_id'],
               'sku_id'=>$goods['sku_id'],
               'uid'=>$data['uid'],
               'order_goods_id'=>$data['order_goods_id'],
               'comment'=> $data['comment'],
               'score'  => $data['grade'],
               'avatar' => $data['avatar'],
               'updated_at' => Carbon::now()->format('Y-m-d H:i:s'),
               'created_at' => Carbon::now()->format('Y-m-d H:i:s'),
           ];
        }
        $flag = self::query()->insert($insertData);

        //将该订单的状态更改为已评价
        $order_model = Order::find($data['order_goods_id']);
        if(!empty($order_model)){
            $order_model -> comment_status = 1;
            $order_model -> save();
        }
        return $flag;
    }


    public static function myComment ($uid)
    {
        $data = self::where ('uid',$uid)
            ->join ('order_goods','order_goods.id','=','user_comments.order_goods_id')
            ->join ('orders','order_goods.order_id','=','orders.id')
            ->join ('user','user_comments.uid','=','user.id')
            ->join ('sku_props_vals','sku_props_vals.sku_id','=','user_comments.sku_id')  #join sku  太可怕了
            ->leftJoin('goods_image','goods_image.goods_id','=','user_comments.goods_id')
            ->leftJoin('props','props.props_id','=','sku_props_vals.props_id')
            ->leftJoin('props_vals','props_vals.props_val_id','=','sku_props_vals.props_val_id')
            ->where('is_default','=',1)
            ->where('sku_props_vals.disabled','=',0)
            ->where('products.disabled','=',0)
            ->select ('user_comments.*','order_goods.goods_price','order_goods.sku_id','order_goods.goods_name','order_goods.goods_number','order_goods.created_at','order_sn','user.user_nickname')
            ->get ()
            ->toArray ();
        #大神这边还需要做一些格式化的处理 将每个sku的属性合并一下,这么复杂的操作就交给大神了
        return $data;
    }
}
